$(function () {
    var pot_customer_datagrid = $("#pot_customer_datagrid");
    var pot_customer_dialog = $("#pot_customer_dialog");
    var transferClient_dialog = $("#transferClient_dialog");
    var cutomerDrain_dialog = $("#cutomerDrain_dialog");
    var pot_customer_form = $("#pot_customer_form");
    var transfer_form = $("#transfer_form");
    var cutomerDrain_form = $("#cutomerDrain_form");
    pot_customer_datagrid.datagrid({
        url: '/client/potentialCustomerList.do?status=0',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        toolbar: '#tb',
        columns: [[
            {field: 'name', title: '客户姓名', width: 100},
            {field: 'age', title: '年龄', width: 100},
            {field: 'gender', title: '性别', width: 100,formatter:function (value) {
                return value ? "男" : "女";
            }},
            {field: 'jobId', title: '职业', width: 100,formatter:function (value) {
                var a;
                if (value){
                    $.ajax({
                        type:"get",
                        url:'/details/selectDetailsByid.do',
                        async:false,
                        cache:false,
                        data:"id="+value,
                        success:function (data) {
                            a = data.name;
                        }
                    })
                    return value ? a : "";
                }
            }},
            {field: 'salaryLevelId', title: '薪资水平', width: 100,formatter:function (value) {
                var a;
                if (value){
                    $.ajax({
                        type:"get",
                        url:'/details/selectDetailsByid.do',
                        async:false,
                        cache:false,
                        data:"id="+value,
                        success:function (data) {
                            a = data.name;
                        }
                    })
                    return value ? a : "";
                }
            }},
            {field: 'tel', title: '电话号码', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'address', title: '客户地址', width: 100},
            {field: 'customerSourceId', title: '客户来源', width: 100,formatter:function (value) {
                var a;
                if (value){
                    $.ajax({
                        type:"get",
                        url:'/details/selectDetailsByid.do',
                        async:false,
                        cache:false,
                        data:"id="+value,
                        success:function (data) {
                            a = data.name;
                        }
                    })
                    return value ? a : "";
                }
            }},
            {field: 'qq', title: 'QQ', width: 100},
            {field: 'wechat', title: '微信', width: 100},
            {field: 'inChargeUser', title: '负责人', width: 100,formatter:function (value) {
                return value ? value.username : "";
            }},
            {field: 'inputUser', title: '创建人', width: 100,formatter:function (value) {
                return value ? value.username : "";
            }},
            {field: 'inputTime', title: '日期', width: 100},
            {field: 'idNo', title: '身份证', width: 100}
        ]]
    })
    pot_customer_dialog.dialog({
        width: 620,
        height: 300,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            pot_customer_form.form('clear');
        }
    })
    transferClient_dialog.dialog({
        width: 350,
        height: 400,
        buttons: '#btn2',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            cutomerDrain_form.form('clear');
            transfer_form.form('clear');
        }
    })
    cutomerDrain_dialog.dialog({
        width: 400,
        height: 300,
        buttons: '#btn3',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            transfer_form.form('clear');
            cutomerDrain_form.form('clear');
        }
    })

    var methodObj = {
        add: function () {
            //设置标题
            pot_customer_dialog.dialog('setTitle', '新增客户');
            //打开弹出框
            pot_customer_dialog.dialog('open');
        },
        edit: function () {

            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            //把数据回显到表单中
            pot_customer_form.form('load', row);
            //设置标题
            pot_customer_dialog.dialog('setTitle', '编辑客户');
            //打开弹出框
            pot_customer_dialog.dialog('open');

        },
        reload: function () {
            pot_customer_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            pot_customer_form.form('submit', {
                url: '/client/potentialCustomerSaveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示',data.msg, 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            pot_customer_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示',data.msg, 'warning');
                    }
                }
            })
        },
        cancel: function () {
            pot_customer_dialog.dialog('close');
        },
        query: function () {
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            var idNo = $("[name=idNo]").val();
            var tel = $("[name=tel]").val();
            var keyword = $("[name=keyword]").val();
            var chargeUserName = $("[name=chargeUserName]").val();
            pot_customer_datagrid.datagrid('load', {
                beginTime: beginTime,
                endTime: endTime,
                idNo: idNo,
                tel: tel,
                keyword: keyword,
                chargeUserName: chargeUserName
            })
        },
        moveIntoClientPool:function () {
            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '确定要将该潜在客户移入资源池吗？', function (r) {
                if (r) {
                    $.get('/client/moveIntoClientPool.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示',data.msg, 'info', function () {
                                pot_customer_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示',data.msg, 'warning');
                        }
                    })
                }
            });
        },
        correctClient:function () {
            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '确定要将该潜在客户转为正式客户吗？', function (r) {
                if (r) {
                    $.get('/client/correctClient.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示',data.msg, 'info', function () {
                                pot_customer_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示',data.msg, 'warning');
                        }
                    })
                }
            });
        },
        transferClient:function () {
            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '选中要操作的数据！', 'warning');
                return;
            }
            console.log(row);
            row["customer.id"] = row.id;
            row["customer.name"] = row.name;
            if (row.inChargeUser){
                row["oldSeller.id"] = row.inChargeUser.id;
                row["oldSeller.username"] = row.inChargeUser.username;
            }
            //把数据回显到表单中
            transfer_form.form('load', row);
            //设置标题
            transferClient_dialog.dialog('setTitle', '移交客户');
            //打开弹出框
            transferClient_dialog.dialog('open');
        },
        cancel2: function () {
            transferClient_dialog.dialog('close');
        },
        save2: function () {
            //提交ajax
            transfer_form.form('submit', {
                url: '/cutomerTransfer/transfer.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示',data.msg, 'info', function () {
                            //关闭弹出框
                            methodObj.cancel2();
                            //在dataGrid重新加载数据
                            pot_customer_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示',data.msg, 'warning');
                    }
                }
            })
        },
        lossOfCustomers:function () {
            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '选中要操作的数据！', 'warning');
                return;
            }
            console.log(row);
            row["customer.id"] = row.id;
            row["customer.name"] = row.name;
            if (row.inChargeUser){
                row["inChargeUser.id"] = row.inChargeUser.id;
                row["inChargeUser.username"] = row.inChargeUser.username;
            }
            //把数据回显到表单中
            cutomerDrain_form.form('load', row);
            $.messager.confirm('确认对话框', '该潜在用户是否已流失吗？', function (r) {
                if (r) {
                    cutomerDrain_dialog.dialog('setTitle', '客户流失');
                    cutomerDrain_dialog.dialog('open');
                }
            });
        },
        cancel3: function () {
            cutomerDrain_dialog.dialog('close');
        },
        save3: function () {
            //提交ajax
            cutomerDrain_form.form('submit', {
                url: '/cutomerDrain/lossOfCustomers.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示',data.msg, 'info', function () {
                            //关闭弹出框
                            methodObj.cancel3();
                            //在dataGrid重新加载数据
                            pot_customer_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示',data.msg, 'warning');
                    }
                }
            })
        },
        developmentFailed:function () {
            var row = pot_customer_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '确定要对该客户进行开发失败操作吗？', function (r) {
                if (r) {
                    $.get('/clientdevplan/developmentFailed.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示',data.msg, 'info', function () {
                                pot_customer_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示',data.msg, 'warning');
                        }
                    })
                }
            });
        }
    }
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
